package com.huas.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huas.common.PageResult;

import com.huas.constant.MessageConstant;
import com.huas.constant.UserConstant;
import com.huas.dto.UserPasswordDTO;
import com.huas.entity.User;
import com.huas.exception.AccountException;
import com.huas.exception.PasswordErrException;
import com.huas.mapper.UserMapper;
import com.huas.service.UserService;

import com.huas.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;


import javax.annotation.Resource;
import java.util.Random;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private UserMapper userMapper;
    @Override
    public PageResult page(Integer page, Integer pageSize, String name) {
        //PageHelper设置分页参数
        PageHelper.startPage(page,pageSize);
        /**
         * 底层实现原理：ThreadLocal
         * startPage:将page,pageSize都存入本地空间，调用mapper的方法又取出进行动态拼接
         */
        //获取的其实是一个ArrayList集合
        Page<User> p = userMapper.page(name);

        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    @Transactional
    public void insert(User user) {
        //补充基础数据
        //会员账号&卡号随机生成
        Random random = new Random();
        String accountTemp = "2024";
        for (int i = 0; i < 4; i++) {
            accountTemp += random.nextInt(10);
        }
        String account = accountTemp;
        //默认密码为123456
        String encode = passwordEncoder.encode(UserConstant.ACCOUNT_PASSWORD);
        user.setAccount(account);
        user.setPassword(encode);

        userMapper.reset();
        userMapper.insert(user);
    }

    @Override
    public User findById(Integer id) {
        User user = userMapper.findById(id);
        return user;
    }

    @Override
    @Transactional
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    @Override
    @Transactional
    public void updatePassword(UserPasswordDTO userPasswordDTO) {
        userPasswordDTO.setId(SecurityUtils.getUserId());
        log.info("要修改密码的用户信息为：{}",userPasswordDTO);
        User user = userMapper.findById(SecurityUtils.getUserId());
        //判断是否存在该用户
        if(ObjectUtils.isEmpty(user)){
            throw new AccountException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //判断该用户是否知道旧密码
        if(!passwordEncoder.matches(userPasswordDTO.getOldPassword(), user.getPassword())){
            throw new PasswordErrException(MessageConstant.PASSWORD_ERROR);
        }
        //将传入的新密码加密
        String newPassword = passwordEncoder.encode(userPasswordDTO.getNewPassword());
        userPasswordDTO.setNewPassword(newPassword);
        userMapper.updatePassword(userPasswordDTO);
    }
}
